﻿using BLL;
using IBLL;
using MODEL;
using Shop.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.Controllers
{
    public class ProductAttrKeySkuController : Controller
    {
        //侯海啸
        IProductAttrKeyBLL attrKeyBLL = new ProductAttrKeyBLL();
        IProductAttrValueBLL attrValueBLL = new ProductAttrValueBLL();
        // GET: ProductAttrKey
        public ActionResult List(int id)
        {
            return View();
        }
        [HttpGet]
        public ActionResult GetByCategoryID(int categoryID) {
            var list = attrKeyBLL.Search(x=>x.ProductCategoryID==categoryID && x.IsSku ==1);
            List<ProductAttrKeyVModel> vList = new List<ProductAttrKeyVModel>();
            foreach (var item in list) {
                var vModel = new ProductAttrKeyVModel();
                vModel.ID = item.ID;
                vModel.AttrName = item.AttrName;
                vModel.EnterType = item.EnterType;
                vModel.AttrValues = new List<string>();
                var attrvalus = attrValueBLL.Search(x=> x.ProductAttrKeyID==item.ID);
                foreach (var valueItem in attrvalus) {
                    vModel.AttrValues.Add(valueItem.AttrValue);
                }
                vList.Add(vModel);
            }
            return Json(vList,JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Add(ProductAttrKeyVModel productAttrKeyVModel)
        {
            ProductAttrKey productAttrKey = new ProductAttrKey()
            {
                //侯海啸
                AttrName = productAttrKeyVModel.AttrName,
                OrderNum = productAttrKeyVModel.OrderNum,
                EnterType = productAttrKeyVModel.EnterType,
                ProductCategoryID = productAttrKeyVModel.ProductCategoryID,
                IsSku = 1
            };
            attrKeyBLL.Add(productAttrKey);
            //插入value表
            if (productAttrKeyVModel.AttrValues != null)
            {
                foreach (var item in productAttrKeyVModel.AttrValues)
                {
                    ProductAttrValue productAttrValue = new ProductAttrValue()
                    {
                        AttrValue = item,
                        ProductAttrKeyID = productAttrKey.ID
                    };
                    attrValueBLL.Add(productAttrValue);
                }
            }

            return Json(new { state = true, msg = "添加成功" });
        }
        public ActionResult GetByCategoryID(int draw, int categoryId)
        {

            var list = attrKeyBLL.GetByCategoryID(categoryId,1);
            var result = new { draw = draw, data = list };
            return Json(result);
        }
        [HttpGet]
        public ActionResult GetOne(int id)
        {

            var attrKey = attrKeyBLL.GetOne(id);
            var attrValue = attrValueBLL.GetAllByAttrKeyID(id);
            var data = new
            {
                attrKey = attrKey,
                attrValue = attrValue
            };
            return Json(data, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        public ActionResult Edit(ProductAttrKeyVModel productAttrKeyVModel)
        {
            ProductAttrKey productAttrKey = new ProductAttrKey()
            {
                //侯海啸
                ID = productAttrKeyVModel.ID,
                AttrName = productAttrKeyVModel.AttrName,
                OrderNum = productAttrKeyVModel.OrderNum,
                EnterType = productAttrKeyVModel.EnterType,
                ProductCategoryID = productAttrKeyVModel.ProductCategoryID,
                IsSku = 1
            };
            List<ProductAttrValue> attrValues = new List<ProductAttrValue>();
            //插入value表
            if (productAttrKeyVModel.AttrValues != null)
            {
                foreach (var item in productAttrKeyVModel.AttrValues)
                {
                    ProductAttrValue productAttrValue = new ProductAttrValue()
                    {
                        AttrValue = item,
                        ProductAttrKeyID = productAttrKey.ID
                    };
                    attrValues.Add(productAttrValue);
                }
            }
            attrKeyBLL.Update(productAttrKey, attrValues);
            return Json(new { state = true, msg = "修改成功" });
        }
        [HttpPost]
        public ActionResult Delete(int id)
        {
            int result = attrKeyBLL.Delete(id);
            return Json(new { State = result > 0 ? true : false });
        }
    }
}